﻿using System;
using System.Collections.Generic;
using System.Text;

namespace SpeakerRecognition
{
    /// <summary>
    /// Class for PreEmphasing the input signal to obtain similar amplitude for all formants
    /// </summary>
    public class PreEmphasisCommand : ICommand
    {
        //************************  Private Fields  *******************************// 
        private double[] outputData;
        private double[] inputData;
        private double alpha = 0.95;
        //************************  Constructors    *******************************// 
        public PreEmphasisCommand()
        {
        }
        /// <summary>
        /// Constructor - get alpha value to Emphasis the signal
        /// </summary>
        /// <param name="alpha"></param>
        public PreEmphasisCommand(double alpha)
        {
            if ((alpha > 0) && (alpha < 1))
                this.alpha = alpha;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="alpha"></param>
        /// <param name="input"></param>
        public PreEmphasisCommand(double alpha, double[] input)
        {
            if ((alpha > 0) && (alpha < 1))
                this.alpha = alpha;
            inputData = input;
            outputData = new double[inputData.Length];
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="input"></param>
        public PreEmphasisCommand(double[] input)
        {
            inputData = input;
            outputData = new double[inputData.Length];
        }
        //************************  Public Properties  ****************************// 
        public double Alpha
        {
            get { return alpha; }
            set { alpha = value; }
        }

        public double[] InputData
        {
            get { return inputData; }
            set { inputData = value;
                outputData = new double[inputData.Length]; }
        }

        public double[] OutputData
        {
            get { return outputData; }
            set { outputData = value; }
        }
        //************************  Public Methods  ****************************// 
        #region ICommand Members

        /// <summary>
        /// Execute emphasis for the input signal to Obtain similar amplitude for all formants
        /// </summary>
        public void execute()
        { 
            outputData[0] = inputData[0];   //first value is the same in both arrays
            for (int i = 1; i < inputData.Length; i++ )
            {
                // X'(n) = X(n) - alpha * X(n-1)
                outputData[i] = inputData[i] - Alpha * (inputData[i - 1]);
            }
        }
        #endregion
    }
}
